//
//  WBLoginViewController.m
//  VoiceLive
//
//  Created by 凸阿滨 on 2017/10/20.
//  Copyright © 2017年 WB. All rights reserved.
//

#import "WBLoginViewController.h"
#import "WBLiveViewController.h"

@interface WBLoginViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *accountTextField;
@property (strong, nonatomic) IBOutlet UITextField *passwordTextField;
@property (strong, nonatomic) IBOutlet UIButton *loginBtn;
@property (strong, nonatomic) IBOutlet UIImageView *backImageView;

@end


@implementation WBLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect statusRect = [[UIApplication sharedApplication] statusBarFrame];
    NSLog(@"高度%f",statusRect.size.height);
   
    
    [self initControl];
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UFUserDefault *userinfo = [UFUserDefault shared];
     [userinfo autoLoginUser];
    
    if(userinfo.account.length > 0){
        self.accountTextField.text = userinfo.account;
        self.passwordTextField.text = userinfo.password;
    }
    
    if (userinfo.exit.length > 0){
        self.accountTextField.text = @"";
        self.passwordTextField.text = @"";
        
    }
    
}



#pragma mark initControl
- (void)initControl{
    self.accountTextField.placeholder = @"帐号";
    self.passwordTextField.placeholder = @"密码";
    self.passwordTextField.secureTextEntry = YES;
    self.accountTextField.delegate = self;
    self.passwordTextField.delegate = self;

    
}

#pragma mark loginAction
- (IBAction)loginAction {
    
    NSString * urlString= [[NetingAPI shared] urlStringWithModuleLoginTag:KUFLogin];
    NSDictionary *parameters = @{@"username":self.accountTextField.text,@"password":self.passwordTextField.text};
    [WBNetworking POST:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(self.accountTextField.text.length == 0 || self.passwordTextField.text.length == 0){
            [self alertMessage:@"请输入帐号密码"];
        }else if ([responseObject[@"code"] isEqualToString:@"200"]){
            //保存信息
         
            UFUserDefault *userInfo = [UFUserDefault shared];
            userInfo.channel_id = responseObject[@"channel_id"];
            userInfo.channel_token = responseObject[@"channel_token"];
            userInfo.redis_token = responseObject[@"redis_token"];
            userInfo.account = self.accountTextField.text;
            userInfo.password = self.passwordTextField.text;
            [userInfo saveUser];
            
            [self showHUD:@"正在登录中"];
            [self performSelector:@selector(loginHome) withObject:self afterDelay:1.0];
            
        }else{
            [self alertMessage:@"登录失败"];
            
        }
        
        
       
      
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
    }];
    
}


#pragma mark loginHome
-(void)loginHome{
    [self hideHUD];
        WBLiveViewController *liveVc = [[WBLiveViewController alloc] init];
        [self presentViewController:liveVc animated:YES completion:nil];
    
}
#pragma mark 键盘下一步
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([self.accountTextField isFirstResponder]) {
        [self.passwordTextField becomeFirstResponder];
    } else if([self.passwordTextField isFirstResponder]) {
        [self loginAction];
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self drapViewUp:textField];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self drapViewDown];
    [self.view endEditing:YES];
    
}

@end
